package com.ce.pms.customer.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.util.Assert;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.customer.dao.IProRightDao;
import com.ce.pms.customer.model.ProRightDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.model.HouseDomain;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
/**
 * 功能描述:
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 * @msg.对应测试类
 * @see
 * @author 付湘辉
 * @Created on 2008-05-27
 */
public class ProRightDaoImpl extends PmsBaseDaoImpl<ProRightDO> implements IProRightDao {

	/**
	 * 功能描述:根据房屋实体查询其有效产权  <br>
	 * @param house 房屋实体对象，只使用到ID字段，可new实例
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-5-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public ProRightDO getEffectiveProRightByHouse(HouseDomain house){
		Assert.notNull(house);
		ProRightDO proRight=null;
		
		proRight = this.getEffectiveProRightByHouseId(house.getKeyword());
		return proRight;
	}

	/**
	 * Function:根据房间的id查找有效产权do
	 * @param houseId 房间的id
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-10
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public ProRightDO getEffectiveProRightByHouseId(Long houseId){
		ProRightDO proRight=null;
		StringBuilder queryStr = new StringBuilder("FROM ProRightDO pro ");
		queryStr.append("WHERE pro.house.id=? AND pro.validity=1 ");
		Query query=super.createQuery(queryStr.toString(), houseId);
		List list = query.list();
		if(list.size()>0) {
			proRight = (ProRightDO)list.get(0);
		}
		return proRight;
	}

	/**
	 * Function:根据楼栋id查找其下所有房间的有效产权
	 * @param buildingId
	 * @return List<ProRightDO>
	 * Create author:胡煦
	 * Create on:2008-11-10
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public Map getEffectiveProRightsByBuildingId(Long buildingId){
		StringBuffer queryStr = new StringBuffer("SELECT proRight.house.id,proRight FROM ProRightDO proRight ");
		queryStr.append("WHERE proRight.house.building.id=? AND proRight.validity=1 ");
		Query query=super.createQuery(queryStr.toString(), buildingId);
		List<Object[]> list = query.list();
		Map map = new HashMap();
		for(Object[] objects : list){
			map.put(objects[0], objects[1]);
		}
		return map;
	}

	/**
	 * Function:根据房间的id查找这个房间的上一次变更的产权do
	 * @param
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-10
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public ProRightDO getLastProRightByHouseId(Long houseId){
		return this.getLastProRightByHouseId(houseId, true);
	}

	/**
	 * Function:根据房间的id查找这个房间的最近一次产权do
	 * @param houseId 房间的id
	 * @param validity 如果为true 只查询无效的产权
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-10
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public ProRightDO getLastProRightByHouseId(Long houseId,boolean validity){
		ProRightDO proRight=null;
		StringBuffer queryStr = new StringBuffer("FROM ProRightDO proRight ");
		queryStr.append("WHERE proRight.house.id=? ");
		if (validity) {
			queryStr.append("AND proRight.validity=0 ");
		}
		queryStr.append("ORDER BY proRight.sysInfo.sysDate desc ");
		Page page =super.pagedQueryByPageNo(queryStr.toString(), 1, 1,new Object[]{houseId});
		List list = page.getResult();
		if(list.size()>0) {
			proRight = (ProRightDO)list.get(0);
		}
		return proRight;
	}

	/**
	 * Function:根据房间的id查找这个房屋(可以是管理房)的最近一次产权do的前一个产权
	 * @param houseId 房间的id
	 * @param validity 如果为true 只查询无效的产权
	 * @return
	 * Create author:xuhaidui
	 * Create on:2008-6-10
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public ProRightDO getLastTwoProRightByHouseId(Long houseId,boolean validity){
		ProRightDO proRight=null;
		StringBuffer queryStr = new StringBuffer("FROM ProRightDO proRight ");
		queryStr.append("WHERE proRight.house.id=? ");
		if (validity) {
			queryStr.append("AND proRight.validity=0 ");
		}
		queryStr.append("ORDER BY proRight.sysInfo.sysDate desc ");
		Page page =super.pagedQueryByPageNo(queryStr.toString(), 1, 2,new Object[]{houseId});
		List list = page.getResult();
		if(list.size() > 1) {
			proRight = (ProRightDO)list.get(1);
		}
		return proRight;
	}

	/**
	 *
	 * 功能描述: 查询除指定房屋外的其它房屋中,所关联的指定合同号或者产权号的数量
	 * 注意:参数contactNO 和 proRightNO 有一个应该为空
	 *
	 * @author 付湘辉
	 * Created on:2008-6-26
	 *
	 * 修改原因：
	 * @param house
	 * @param contactNO
	 * @param proRightNO
	 * @return Integer 返回结果
	 */
	public Integer getProRightCountByHouse(HouseDomain house,String contactNO,String proRightNO){
		Integer result = null;

		if (BlankUtil.isBlank(house)) {
			return null;
		}

		Criteria cri = null;
		if (!BlankUtil.isBlank(proRightNO)) {
			cri = super.crtieriaFindBy("proRightNO", proRightNO);
		}else if (!BlankUtil.isBlank(contactNO)) {
			cri = super.crtieriaFindBy("contactNO", contactNO);
		}else{
			return null;
		}

		cri.add(Restrictions.eq("validity", SysInfo.VALIDITY));
		cri.add(Restrictions.ne("house", house));

		result = super.queryObjects(cri).size();

		return result;
	}

	/**
	 *
	 * 功能描述: 添加产权
	 *
	 * @author 付湘辉
	 * Created on:2008-6-10
	 *
	 * 修改原因：
	 * @param proRight
	 * @return
	 * @return ProRightDO 返回结果
	 */
	public ProRightDO saveProRight(ProRightDO proRight){
		return super.saveObject(proRight);
	}
}
